package com.example.email.mailTest;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/2/8 14:12
 */
public class MailTest {
    //@Autowired
    private static  JavaMailSenderImpl mailSender;

    public static boolean sendMail(String to, String text, String title) throws MessagingException {

        return false;
    }

    public static void main(String[] args) throws Exception { // 做测试用

        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");

        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.port", "587");
        // 开启SSL加密，否则会失败
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        props.put("mail.username","1992353533@qq.com");
        //mailSender.setUsername();
//        mailSender.setPassword("pxbnkzkeotlqcbcb");
        // 创建session
        Session session = Session.getInstance(props);

        // 创建邮件
        Message msg = new MimeMessage(session);
        // 设置标题
        msg.setSubject("测试邮件");
        // 编辑内容
        StringBuilder builder = new StringBuilder();
        builder.append("这是一封java mail测试邮件\n");
        builder.append("这是第二行");
       // builder.append("\n时间 " + getStringDate());
        // 设置内容
        msg.setText(builder.toString());
        // 发送的邮箱地址
        msg.setFrom(new InternetAddress("1992353533@qq.com"));
        // 通过session得到transport对象
        Transport transport = session.getTransport();
        // 连接邮件服务器：邮箱类型，帐号，授权码代替密码（更安全）
        transport.connect("smtp.qq.com", "1992353533@qq.com", "");
        // 发送邮件
        transport.sendMessage(msg, new Address[] { new InternetAddress("1453372772@qq.com") });
        transport.close();




//        mailSender = new JavaMailSenderImpl();
//        mailSender.setHost("smtp.qq.com");  //设置邮件服务器
//        mailSender.setUsername("1992353533@qq.com");
//        mailSender.setPassword("pxbnkzkeotlqcbcb");
//
//        MimeMessage msg = mailSender.createMimeMessage();
//        MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "utf-8");
//
//        msgHelper.setTo("1453372772@qq.com");
//        msgHelper.setFrom("1992353533@qq.com");
//        msgHelper.setSubject("测试发送带附件的邮件");
//        msgHelper.setText("测试邮件");
//
////        FileSystemResource file = new FileSystemResource(new File("D:/test.png"));
////        msgHelper.addAttachment("test.png", file); //添加附件
//
//        Properties prop = new Properties();
//        prop.put("mail.smtp.auth", "true");
//        prop.put("mail.smtp.timeout", "25000");
//        mailSender.setJavaMailProperties(prop);
//
//        mailSender.send(msg);
//
//        System.out.println("邮件发送成功！");
    }
}
